package com.gzy.literature.dao;

import com.gzy.literature.entity.Works;
import org.apache.ibatis.annotations.Param;
import org.springframework.data.domain.Pageable;
import java.util.List;


import org.apache.ibatis.annotations.*;
import org.springframework.data.domain.Pageable;

import java.util.List;

public interface WorksDao {

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Select("SELECT * FROM works WHERE id = #{id}")
    Works queryById(Integer id);

    /**
     * 查询指定条件的数据（可能用于分页）
     *
     * @param works 查询条件（可以为空，用于动态查询）
     * @return 对象列表
     */
    @Select("<script>" +
            "SELECT * FROM works WHERE 1=1 " +
            "<if test='works.title != null'> AND title LIKE CONCAT('%', #{works.title}, '%')</if>" +
            "<if test='works.introduction != null'> AND introduction LIKE CONCAT('%', #{works.introduction}, '%')</if>" +
            "</script>")
    List<Works> queryBll(@Param("works") Works works);

    /**
     * 统计总行数
     *
     * @param works 查询条件（可以为空，用于动态查询）
     * @return 总行数
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM works WHERE 1=1 " +
            "<if test='works.title != null'> AND title LIKE CONCAT('%', #{works.title}, '%')</if>" +
            "<if test='works.introduction != null'> AND introduction LIKE CONCAT('%', #{works.introduction}, '%')</if>" +
            "</script>")
    long countByCondition(@Param("works") Works works);

    /**
     * 新增数据
     *
     * @param works 实例对象
     * @return 影响行数
     */
    @Insert("INSERT INTO works(introduction, title, publishTime, content, cover) VALUES(#{introduction}, #{title}, #{publishTime}, #{content}, #{cover})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Works works);

    /**
     * 修改数据
     *
     * @param works 实例对象
     * @return 影响行数
     */
    @Update({
            "<script>",
            "UPDATE works ",
            "<set>",
            "<if test='introduction != null'>introduction = #{introduction},</if>",
            "<if test='title != null'>title = #{title},</if>",
            "<if test='publishTime != null'>publishTime = #{publishTime},</if>",
            "<if test='content != null'>content = #{content},</if>",
            "<if test='cover != null'>cover = #{cover},</if>",
            "</set>",
            "WHERE id = #{id}",
            "</script>"
    })
    int update(Works works);

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 影响行数
     */
    @Delete("DELETE FROM works WHERE id = #{id}")
    int deleteById(Integer id);
}